package com.ayx.redispublish.config;

import com.ayx.redispublish.Listener.PrintMessageReceiver;
import com.ayx.redispublish.Listener.RedisMessageListener;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.Arrays;

@Configuration
public class RedisConfig {

    /**
     * redistemplate配置类
     */

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        //实际开发中我们常用的一般都是<String,Object>这种形式
        RedisTemplate<String, Object> template = new RedisTemplate<>();

        //设置连接工厂
        template.setConnectionFactory(factory);

        //Json序列化器配置

        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(mapper);
        //设置redis的key采用string的序列化
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        template.setStringSerializer(stringSerializer);

        //设置hash结构的小key也是String类型的序列化
        template.setHashKeySerializer(stringSerializer);
        //设置Value为JSON序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //设置hash结构的Value采用JSON方式的序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        //使设置生效
        template.afterPropertiesSet();
        return template;
    }


    /**第一中监听器方式
     * 配置redis消息监听容器，此容器可以添加多个消息监听器
     */
    private final static String TOPPIC = "TOPIC_1";
    private final static String TOPPIC2 = "TOPIC_2";

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory, RedisMessageListener redisMessageListener,MessageListenerAdapter adapter) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        //设置消息订阅的主题
        redisMessageListenerContainer.addMessageListener(redisMessageListener, new PatternTopic(TOPPIC));
        redisMessageListenerContainer.addMessageListener(adapter, new PatternTopic(TOPPIC2));
        //监听所有库的key过期事件
        redisMessageListenerContainer.setConnectionFactory(factory);

        /**
         * 设置序列化对象
         * 特别注意：1. 发布的时候需要设置序列化；订阅方也需要设置序列化
         *         2. 设置序列化对象必须放在[加入消息监听器]这一步后面，否则会导致接收器接收不到消息
         */
        Jackson2JsonRedisSerializer seria = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        seria.setObjectMapper(objectMapper);
        redisMessageListenerContainer.setTopicSerializer(seria);

        return redisMessageListenerContainer;
    }

    /**
     *  第二种方式适配器方式
     * @param printMessageReceiver
     * @return
     */
    @Bean
    public MessageListenerAdapter listenerAdapter(PrintMessageReceiver printMessageReceiver) {
        MessageListenerAdapter receiveMessage = new MessageListenerAdapter(printMessageReceiver, "receiveMessage");

        Jackson2JsonRedisSerializer seria = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        seria.setObjectMapper(objectMapper);
        receiveMessage.setSerializer(seria);
        return receiveMessage;
    }
}
